Developer Documentation
PATH  Mac OS X Documentation > Cocoa > Application Design for Scripting, Documents, and Undo


Previous | Chapter contents | Next | Book PDF

Undo and Redo

The Yellow Box frameworks provide support for implementing undo and redo. NSUndoManager objects are responsible for tracking of the actions necessary to undo changes that are made to a document. The basic premise of the undo architecture is that when you are about to do something you first tell the NSUndoManager how to undo it. The main API is invocation based, so if you have a setColor: method, it sends a message similar to the following before it actually sets the new color:

[[undoManager prepareWithInvocationTarget:self] setColor:oldColor]

This message causes the creation of an NSInvocation; if the user chooses Undo, that invocation (of the method setColor: with the parameter being the old color) is invoked. Since undone changes are put on a redo stack, if the user chooses the Redo command, the changes are redone.

Because many discrete changes might be involved in a user-level action, all the undo registrations that happen during a single cycle of the event loop are usually grouped together and are undone all at once. NSUndoManager has methods that allow you to control the grouping behavior further if you need to.

Undo and the Document Architecture

Undo and the Model Layer

Undo and the Control and View Layers

Undo and Scripting


Application Design for Scripting, Documents, and Undo

Previous | Chapter contents | Next | Book PDF